<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class admin_controller_language extends omnitrix_controller
{
	public function index_action()
	{
		$rows = $this->_fetch_language_list();

		$list_table = otx_html_create_table($this->_('heading_index'), 'table-list');
		$list_table->set_head_cell($this->_('column_language_name'));
		$list_table->set_head_cell($this->_('column_language_status'), array('class' => 'center', 'width' => 150));
		$list_table->set_head_cell($this->_('column_installed_time'), array('class' => 'center', 'width' => 150));
		$list_table->set_head_cell($this->_('column_installed_user'), array('class' => 'center', 'width' => 150));
		$list_table->set_head_cell($this->_('text_actions'), array('class' => 'center', 'width' => 150));

		if ($rows) {
			foreach ($rows as $row) {
				$title = $row['title'];
				$description = empty($row['description']) ? '' : $row['description'];
				$website = empty($row['website']) ? '' : $row['website'];
				$email = empty($row['email']) ? '' : $row['email'];
				$author = empty($row['author']) ? $email : $row['author'];
				$version = empty($row['version']) ? '' : $row['version'];

				if ($website) {
					$language_detail = otx_html_strong(otx_html_link($title, array('href' => $row['website'], 'target' => '_blank')));
				} else {
					$language_detail = otx_html_strong($title);
				}

				if ($version) {
					$language_detail .= '&nbsp;(' . $version . ')';
				}

				if ($description) {
					$language_detail .= '<br />' . otx_html_small($description);
				}

				if ($author) {
					$language_detail .= '<br />'
							. otx_html_tag_start('i')
							. otx_html_tag_start('small')
							. $this->_('text_author');

					if ($email) {
						$language_detail .= otx_html_tag_simple('a', $author, array('href' => 'mailto:' . $email));
					} else {
						$language_detail .= $author;
					}

					$language_detail .= otx_html_tag_end('small')
							. otx_html_tag_end('i');
				}

				$actions = otx_html_tag_custom('dropdown');
				if (isset($row['language_id'])) {
					$actions->add_item($this->_('action_uninstall'), otx_router_url('uninstall', null, null, array('id' => $row['language_id'])));
				} else {
					$actions->add_item($this->_('action_install'), otx_router_url('install', null, null, array('name' => $row['language_name'])));
				}

				$status = array();
				if (isset($row['language_id'])) {
					if (otx_config_string('system_index_language') == $row['language_name']) {
						$status[] = otx_html_span($this->_('text_default_index'), array('class' => 'italic'));
					} else {
						$actions->add_item($this->_('action_apply_index'), otx_router_url('apply', null, null, array('type' => 'index', 'id' => $row['language_id'])));
					}
					if (otx_config_string('system_admin_language') == $row['language_name']) {
						$status[] = otx_html_span($this->_('text_default_admin'), array('class' => 'italic'));
					} else {
						$actions->add_item($this->_('action_apply_admin'), otx_router_url('apply', null, null, array('type' => 'admin', 'id' => $row['language_id'])));
					}
					if (empty($status)) {
						$status[] = otx_html_span($this->_('text_installed'));
					}
				} else {
					$status[] = otx_html_span($this->_('text_notinstalled'), array('class' => 'bold red'));
				}

// 				if (file_exists(otx_language_path($row['language_name'], 'thumbnails/index.png'))) {
// 					$is_default = otx_config_string('system_index_language') == $row['language_name'];
// 					$thumbnail_index = otx_html_tag_empty('img', array('src' => otx_language_url($row['language_name'], 'thumbnails/index.png'), 'class' => 'language-thumbnail' . ($is_default ? ' default-thumbnail' : ''), 'width' => 150, 'height' => 120))
// 							. ($is_default ? '<br/>' . $this->_('text_default_index') : '');
// 					if (isset($row['language_id']) && !$is_default) {
// 						$actions->add_item($this->_('action_apply_index'), otx_router_url('apply', null, null, array('type' => 'index', 'id' => $row['language_id'])));
// 					}
// 				} else {
// 					$thumbnail_index = $this->_('text_not_support');
// 				}

// 				if (file_exists(otx_language_path($row['language_name'], 'thumbnails/admin.png'))) {
// 					$is_default = otx_config_string('system_admin_language') == $row['language_name'];
// 					$thumbnail_admin = otx_html_tag_empty('img', array('src' => otx_language_url($row['language_name'], 'thumbnails/admin.png'), 'class' => 'language-thumbnail' . ($is_default ? ' default-thumbnail' : ''), 'width' => 150, 'height' => 120))
// 							. ($is_default ? '<br/>' . $this->_('text_default_admin') : '');
// 					if (isset($row['language_id']) && !$is_default) {
// 						$actions->add_item($this->_('action_apply_admin'), otx_router_url('apply', null, null, array('type' => 'admin', 'id' => $row['language_id'])));
// 					}
// 				} else {
// 					$thumbnail_admin = $this->_('text_not_support');
// 				}

				$list_table->add_body_row();
				$list_table->set_body_cell($language_detail);
				$list_table->set_body_cell(implode('<br/>', $status), array('class' => 'center'));
				$list_table->set_body_cell(isset($row['language_id']) ? otx_string_format_datetime($row['installed_time']) : '&nbsp;', array('class' => 'center'));
				$list_table->set_body_cell(isset($row['language_id']) ? otx_html_link($row['installed_user_name'], array('href' => otx_router_url('edit', 'user', 'admin', array('id' => $row['installed_user_id'])))) : '&nbsp;', array('class' => 'center'));
				$list_table->set_body_cell($actions->render_link($this->_('text_options')), array('class' => 'center actions'));
			};
		} else {
			$list_table->add_body_row();
			$list_table->set_body_cell($this->_('text_no_results'), array('colspan' => 5));
		}

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc', DIR_LANGUAGES), otx_router_url('index'), true);
		otx_html_add_breadcrumb($this->_('breadcrumb_index'));
		otx_html_response_title($this->_('breadcrumb_index'));
	}

	public function install_action()
	{
		$language_name = otx_param_string('name');
		$language_instance = otx_language_instance($language_name);

		$row['language_name'] = $language_name;
		$row['installed_time'] = $GLOBALS['_XTIMESTAMP'];
		$row['installed_user_id'] = otx_session_user_id();
		otx_db_insert('#system_language#', $row);
		$row['language_id'] = otx_db_last_inserted_id();

		$language_instance->language_id = $row['language_id'];
		$language_instance->install();
		otx_event_dispatch('language_install', array('language_name' => $language_name));

		otx_cache_delete('languages');

		$language_info = array_merge($row, otx_language_info($language_name));
		otx_admin_flash_message($this->_('text_success_install', $language_info));
		otx_admin_log_action($this->_('text_success_install', $language_info));
		otx_router_redirect('index');
	}

	public function uninstall_action()
	{
		$language_id = otx_param_int('id');

		$sql = 'SELECT * FROM #system_language# WHERE language_id = ?';
		$row = otx_db_fetch_row($sql, $language_id);
		if (false == $row) {
			otx_admin_flash_warning($this->_('error_invalid_id', $language_id));
			otx_router_redirect('index');
		}

		if (in_array($row['language_name'], array(otx_config_string('system_index_language'), otx_config_string('system_admin_language')))) {
			otx_admin_flash_warning($this->_('error_default_language'));
			otx_router_redirect('index');
		}

		$language_instance = otx_language_instance($row['language_name']);

		$language_instance->uninstall();
		otx_db_delete('#system_language#',array('language_id' => $language_id));
		otx_event_dispatch('language_uninstall', array('language_name' => $row['language_name']));

		otx_cache_delete('languages');

		$language_info = array_merge($row, otx_language_info($row['language_name']));
		otx_admin_flash_message($this->_('text_success_uninstall', $language_info));
		otx_admin_log_action($this->_('text_success_uninstall', $language_info));
		otx_router_redirect('index');
	}

	public function apply_action()
	{
		$language_id = otx_param_int('id');
		$apply_type = otx_param_string('type');

		$sql = 'SELECT * FROM #system_language# WHERE language_id = ?';
		$row = otx_db_fetch_row($sql, $language_id);
		if (false == $row) {
			otx_admin_flash_warning($this->_('error_invalid_id', $language_id));
			otx_router_redirect('index');
		}

		otx_db_replace('#system_config#', array('name' => 'system_' . $apply_type . '_language', 'value' => $row['language_name'], 'serialized' => 0));
		otx_cache_delete('config');

		$language_info = array_merge($row, otx_language_info($row['language_name']));
		otx_admin_flash_message($this->_('text_success_apply_' . $apply_type, $language_info));
		otx_admin_log_action($this->_('text_success_apply_' . $apply_type, $language_info));
		otx_router_redirect('index');
	}

	protected function _fetch_language_list()
	{
		$language_list = array();

		$sql = 'SELECT p.*, u.user_name AS installed_user_name
				FROM #system_language# p
				LEFT JOIN #system_user# u ON u.user_id = p.installed_user_id';
		$rows = otx_db_fetch_all($sql);
		if ($rows) {
			foreach ($rows as $row) {
				$language_info = otx_language_info($row['language_name']);
				$language_list[$row['language_name']] = array_merge($row, $language_info);
			}
		}

		$dirs = glob(DIR_LANGUAGES . DS . '*');
		if ($dirs) {
			foreach ($dirs as $dir) {
				if (!is_dir($dir)) {
					continue;
				}

				$language_name = basename($dir);
				if (array_key_exists($language_name, $language_list)) {
					continue;
				}

				$language_info = otx_language_info($language_name);
				$language_info['language_name'] = $language_name;
				$language_list[$language_name] = $language_info;
			}
		}

		ksort($language_list);
		return $language_list;
	}
}